import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class demo01 {
    public static void main(String[] args) throws InterruptedException {
//        test();

//        test1();

//        test2();

//        test3();

//        test4();

        test5();
    }
    public static void test() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度
        webDriver.get("https://www.baidu.com");
//        //找到百度搜索输入框
//        WebElement element=webDriver.findElement(By.cssSelector(".s_ipt"));
//        //输入
//        element.sendKeys("软件测试");

        WebElement element=webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        element.sendKeys("软件测试");

        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        //效验
        //找到搜索结果
        List<WebElement> elements=webDriver.findElements(By.cssSelector("a em"));
        int flag=0;
        for (int i = 0; i < elements.size(); i++) {
            if(elements.get(i).getText().equals("测试")||elements.get(i).getText().equals("通过")){
                flag=1;
                System.out.println("测试通过");
                break;
            }
        }
        if(flag==0){
            System.out.println("测试不通过");
        }
    }

    public static void test1() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        sleep(2000);
        //找到百度搜索框输入bilibile
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("bilibili");
        //点击百度一下
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(2000);
        //清空百度搜索框
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    public static void test2(){
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");

        //这个方法会报错，因为点击的是新闻链接
        //这个超链接没有放到form标签中
        //webDriver.findElement(By.xpath("//a[text()=\"新闻\"]")).submit();

        webDriver.findElement(By.xpath("//a[text()=\"新闻\"]")).click();
    }

    public static void test3(){
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");

//        String button_value = webDriver.findElement(By.cssSelector("#su")).getText();
//        //为空
//        System.out.println(button_value);

        //getAttribute来获取
        String button_value = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if(button_value.equals("百度一下")){
            System.out.println("测试通过");
        }else{
            System.out.println(button_value);
            System.out.println("测试不通过");
        }
    }

    public static void test4() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");
        sleep(2000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("bilibili");
        webDriver.findElement(By.cssSelector("#su")).click();
//        //强制等待，死等
//        sleep(2000);

        //隐式等待，等到后就会执行下面的代码
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);

        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    public static void test5(){
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com");

        String url=webDriver.getCurrentUrl();
        String title=webDriver.getTitle();
        if(url.equals("https://www.baidu.com/")&&title.equals("百度一下，你就知道")){
            System.out.println("当前页面url："+url+", 当前页面title："+title);
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");
        }
    }

}